<template>
  <a-button
    type="link"
    style="padding: 4px 6px"
    @click="onClick"
    :disabled="props.data.userStatus === 'resign'"
  >
    编辑
  </a-button>
  <user-modal
    v-model:open="editOpen"
    v-model:title="editTitle"
    :context-data="propsModalData"
    @edit="handleEdit"
  />
</template>
<script setup>
import { editUser } from '@/api/user.js'
import UserModal from './table-new-modal.vue'
import { message } from 'ant-design-vue'
import { ref } from 'vue'
import { useDataSourceManager } from './provide-context.js'

/**
 * 全局数据
 */
let { update } = useDataSourceManager()
let propsModalData = ref({})
let editOpen = ref(false)
let editTitle = ref('编辑人员')
/**
 * 外部参数
 */
let props = defineProps({
  data: { type: Object, required: true },
})

/**
 * 编辑操作
 */
function onClick() {
  editOpen.value = true
  propsModalData.value = {
    btnType: 'edit',
    data: props.data,
  }
}

async function handleEdit(data) {
  try {
    // 只有工作状态为在职时，实习日期不存在
    if (data.userStatus === 'job') {
      data.internshipEndDate = null
    }
    await editUser(data)
    await update()
    message.success('修改成功')
  } catch (e) {
    message.error('修改失败')
  }
}
</script>
